package com.neusoft.shry.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.shry.dao.EmpDao;
import com.neusoft.shry.dto.EmpDto;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.Pager;
import com.zero.mms.model.Employee;
import com.zero.mms.model.Position;

@Service("empService")
@Transactional(rollbackFor = Exception.class)
public class EmpServiceImp implements EmpService {
	
	@Inject
	private EmpDao empDao;

	//get an employee by id
	@Override
	public Employee getEmpById(Integer emp_id) throws SmsRuntimeException {
		try{
			if("".equals(emp_id) || emp_id == null){
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "员工不存在");
			}
			Employee employee = empDao.getEmpById(emp_id);
			return employee;
		}catch (SmsRuntimeException e) {
			throw e;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	//get employees by conditions
	@Override
	public Pager<EmpDto> getEmpByConditions(Integer emp_id,String emp_name,String dep_name)
			throws SmsRuntimeException {
		try {
			if("*".equals(emp_id)||"".equals(emp_id)){
				emp_id = null;
			}
			if("*".equals(emp_name)||"".equals(emp_name)){
				emp_name = null;
			}
			if("*".equals(dep_name)){
				dep_name = "";
			}
			Pager<EmpDto> pager = empDao.getEmpByCondition(emp_id, emp_name, dep_name);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	//add an employee
	@Override
	public void addEmp(Employee employee) throws SmsRuntimeException {
		try {
			empDao.add(employee);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
		
	}

	//delete an employee
	@Override
	public void deleteEmp(Integer emp_id) throws SmsRuntimeException {
		try{
			if("".equals(emp_id) || emp_id == null){
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "员工不存在");
			}
			empDao.delete(emp_id);
		}catch (SmsRuntimeException e) {
			throw e;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
		
	}

	//update an employee
	@Override
	public void updateEmp(Employee employee) throws SmsRuntimeException {
		try {
			empDao.update(employee);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
		
	}

	//get positions of a department
	@Override
	public List<Position> getDepPos(Integer dep_id) throws SmsRuntimeException {
		try{
			if("".equals(dep_id) || dep_id == null){
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "部门不存在");
			}
			List<Position> positions = empDao.getDepPos(dep_id);
			return positions;
		}catch (SmsRuntimeException e) {
			throw e;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

}
